package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.ScreenShot;
import cn.nubia.admin.common.constant.TFEnum;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.utils.DateUtil;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by justin on 15/7/2.
 */
@Service
public class ScreenshotService extends BaseServiceImpl<ScreenShot> {


    /***
     * 假删除
     * @param id
     */
    public void disableScreenshot(Integer id) {
        ScreenShot screenShot = get(id);
        screenShot.setIsDelete(TFEnum.FALSE.getValue());
        update(screenShot);
    }

    public void insertScreen(ScreenShot screenShot){
        try {
            BufferedImage bufferedImage = ImageIO.read(new File(screenShot.getLocalScreenshotUrl()));
            screenShot.setCreateTime(DateUtil.getCurrentTime());
            screenShot.setScreenWidth(bufferedImage.getWidth());
            screenShot.setScreenHeight(bufferedImage.getHeight());
            screenShot.setIsDelete(TFEnum.TRUE.getValue());
            insert(screenShot);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**根据softItemId获取screenshot列表
     *
     * @param softItemId
     * @return
     */
    public List<ScreenShot> findBySoftItemId(Integer softItemId) {
        Query<ScreenShot> query = Query.build(ScreenShot.class);
        query.addEq(ScreenShot.COLUMN_SOFTITEMID,softItemId);
        query.addOrder(ScreenShot.COLUMN_SORTINDEX, Query.DBOrder.ASC);
        return findByQuery(query);
    }
}
